<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\bookdata;


use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\auth\AdminLogic;
use app\common\model\bookdata\Bookdata;
use app\common\lists\ListsSearchInterface;
use app\common\service\FileService;


/**
 * Bookdata列表
 * Class BookdataLists
 * @package app\adminapi\lists
 */
class BookdataLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/06/18 15:02
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['book.book_name'],
            '=' => ['platform', 'promotion_link_id', 'date', 'book_id'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/06/18 15:02
     */
    public function lists(): array
    {
        $sort = $this->params['sort']??3;
        switch ($sort) {
            case 1:
                $order = 'payment_amount desc';//充值金额
                break;
            case 2:
                $order = 'consumer_amount desc';//消费金额
                break;
            case 3:
                $order = 'readnum desc';//阅读次数
                break;
            case 4:
                $order = 'number_of_readers desc';//阅读人数
                break;
            case 5:
                $order = 'consumer_users desc';//消费用户数
                break;
            case 6:
                $order = 'payment_users desc';//充值人数
                break;
            case 7:
                $order = 'payment_num desc';//充值次数
                break;
            default:
                $order = 'readnum desc';//阅读次数
                break;
        }
        $bookdataList = Bookdata::alias('bookdata')->leftJoin('book', 'bookdata.book_id = book.id')->where($this->searchWhere)->where($this->queryWhere())
            ->field(['bookdata.id', 'bookdata.platform', 'bookdata.promotion_link_id', 'bookdata.admin_id', 'bookdata.date', 'bookdata.book_id', 'sum(bookdata.readnum) as readnum', 'sum(bookdata.number_of_readers) as number_of_readers', 'sum(bookdata.consumer_amount) as consumer_amount', 'sum(bookdata.consumer_users) as consumer_users', 'sum(bookdata.payment_users) as payment_users', 'sum(bookdata.payment_num) as payment_num', 'sum(bookdata.payment_amount) as payment_amount','book.book_name','book.thumb'])
            ->group('book_id')
            ->limit($this->limitOffset, $this->limitLength)
            ->order($order)
            ->select();

        foreach ($bookdataList as $item){
            $item['consumption_rate'] = getRate($item->number_of_readers, $item->consumer_users); // 消费率
            $item['payment_rate'] = getRate($item->number_of_readers, $item->payment_users); // 充值率
            $item['per_capita_consumption_amount'] = getRate($item->consumer_amount, $item->consumer_users); // 人均消费金额
            $item['per_capita_payment_amount'] = getRate($item->payment_amount, $item->payment_users); // 人均充值金额
            $item['per_capita_recharge_frequency'] = getRate($item->payment_users, $item->payment_num); // 人均充值次数
            $item['per_capita_reading_frequency'] = getRate($item->readnum, $item->number_of_readers); // 人均阅读次数
            $item['thumb'] = FileService::getFileUrl($item['thumb']);
        }
        return $bookdataList->toArray();
    }

    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];
        if ($this->adminInfo['root'] != 1) {
            $pAdminIds = AdminLogic::getPermissionAdminIds($this->adminInfo);
            if ($pAdminIds) {
                $where[] = ['admin_id', 'in', $pAdminIds];
            }
        }
        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/06/18 15:02
     */
    public function count(): int
    {
        return Bookdata::alias('bookdata')->leftJoin('book', 'bookdata.book_id = book.id')->where($this->searchWhere)->where($this->queryWhere())
            ->field(['bookdata.id', 'bookdata.platform', 'bookdata.promotion_link_id', 'bookdata.admin_id', 'bookdata.date', 'bookdata.book_id', 'sum(bookdata.readnum) as readnum', 'sum(bookdata.number_of_readers) as number_of_readers', 'sum(bookdata.consumer_amount) as consumer_amount', 'sum(bookdata.consumer_users) as consumer_users', 'sum(bookdata.payment_users) as payment_users', 'sum(bookdata.payment_num) as payment_num', 'sum(bookdata.payment_amount) as payment_amount','book.book_name','book.thumb'])
            ->group('book_id')
            ->select()->count();
    }

}